package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.ArticlePageQueryDTO;
import com.sky.mapper.HomeMapper;
import com.sky.result.PageResult;
import com.sky.service.HomeService;
import com.sky.vo.AnnouncementsVO;
import com.sky.vo.ArticleChildCommentVo;
import com.sky.vo.ArticleInfoVO;
import com.sky.vo.HomeOtherVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 贾晓晨
 * @version 1.0
 */
@Slf4j
@Service
public class HomeServiceImp implements HomeService {
    @Autowired
    private HomeMapper homeMapper;

    @Override
    public List<String> getTagList() {
        return homeMapper.getTagList();
    }

    @Override
    public PageResult getArticleList(ArticlePageQueryDTO articlePageQueryDTO) {
        // 开启分页查询
        PageHelper.startPage(articlePageQueryDTO.getPage(), articlePageQueryDTO.getPageSize());
        Page<ArticleInfoVO> articleList = homeMapper.getArticleList(articlePageQueryDTO);
        long total = articleList.getTotal();
        List records = articleList.getResult();
        return new PageResult(total,records);
    }

    @Override
    public HomeOtherVO getOther() {
        // // 获取文章的数量
        Integer articleCount = homeMapper.getArticleCount();
        // 获取视频的数量
        Integer videoCount = homeMapper.getRadioCount();
        // 获取评论的数量
        Integer commentCount = homeMapper.getCommentCount();
        // 获取最新资讯
        List<AnnouncementsVO> information = homeMapper.getNewinformation();
        // 获取前十条评论
        List<ArticleChildCommentVo> newComments = homeMapper.getNewComments();
        // 构建统计数据（带空值保护）

        HomeOtherVO.HomeCountVO homeCountVO = new HomeOtherVO.HomeCountVO().builder()
                .articleCount(articleCount)
                .commentCount(commentCount)
                .radioCount(videoCount)
                .build();

        HomeOtherVO.HomeInfo homeInfo = new HomeOtherVO.HomeInfo().builder()
                .information(information)
                .newComment(newComments)
                .build();

        HomeOtherVO homeOtherVO = new HomeOtherVO().builder()
                .info(homeInfo)
                .countVO(homeCountVO)
                .build();
        return homeOtherVO;
    }

}
